﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;
namespace SDLITS.Web.SystemManage
{
    public partial class AnnouncementChange : CommonBase
    {
        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        #endregion
        
        #region 读取数据


        private void LoadData()
        {
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                Announcement Tm = SDDbSession.Default.From<Announcement>().Where(Announcement._.AnnID == id).ToFirst();
                txt_title.Text = Tm.Title;
                txt_Description.Text = Tm.Description;
                txt_end.SelectedDate = Tm.EndDate;
                txt_start.SelectedDate = Tm.StartDate;
                cbl_ishot.SelectedValue = Boolean.Parse(Tm.IsHot.ToString()) ? "1" : "2"; 
            }
        }
        #endregion

        #region 保存数据
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Announcement Tm = new Announcement();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Tm.AnnID = int.Parse(Request["id"]);
                Tm.Attach();
                Tm.EndDate = txt_end.SelectedDate;
                Tm.Description = txt_Description.Text;
                Tm.IsHot = cbl_ishot.SelectedValue == "1" ? true : false;
                Tm.StartDate = txt_start.SelectedDate;
                Tm.Title = txt_title.Text;
                returnvalue = SDDbSession.Default.Update<Announcement>(Tm);
            }
            else
            {
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                Tm.DeAttach();
                Tm.EndDate = txt_end.SelectedDate;
                Tm.Description = txt_Description.Text;
                Tm.IsHot = cbl_ishot.SelectedValue == "1" ? true : false;
                Tm.StartDate = txt_start.SelectedDate;
                Tm.Title = txt_title.Text;
                Tm.StatusID =1;
                Tm.CreateDate = DateTime.Now;
                Tm.CreateUserID = user.UserID;
                returnvalue = SDDbSession.Default.Insert<Announcement>(Tm);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        #endregion
    }
}